﻿using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Web.UI.WebControls;

namespace Class.Web.Mvc.Image
{
    public static class ProcessScaleImage
    {
        private static Bitmap CreateThumbnail(int width, int height, System.Drawing.Image image, ScaleType scaleType, ReverseSize reverseSize, Brush brush)
        {
            if (width == 0 && height == 0)
            {
                width = image.Width;
                height = image.Height;
            }
            int lnNewHeight;
            int lnNewWidth;
            if (width >= image.Width && height >= image.Height)
            {
                lnNewWidth = image.Width;
                lnNewHeight = image.Height;
            }
            else
            {
                if (height != 0 && width != 0)
                {
                    double maxImageSource = image.Width > image.Height ? image.Width : image.Height;
                    double minImageScale = width > height ? height : width;
                    minImageScale--;
                    do
                    {
                        minImageScale++;
                        lnNewWidth = (int)(image.Width * (minImageScale / maxImageSource));
                        lnNewHeight = (int)(image.Height * (minImageScale / maxImageSource));
                    } while (lnNewHeight <= height && lnNewWidth <= width && lnNewHeight <= image.Height && lnNewWidth <= image.Width);
                }
                else if (height == 0)
                {
                    lnNewWidth = width;
                    lnNewHeight = (int)((lnNewWidth * 1.0d / image.Width) * image.Height);
                }
                else
                {
                    lnNewHeight = height;
                    lnNewWidth = (int)((lnNewHeight * 1.0d / image.Height) * image.Width);
                }
            }

            int x = 0, y = 0;
            if (width != 0 && height != 0)
            {
                if (scaleType == ScaleType.TopLeft)
                {
                    x = 0;
                    y = 0;
                }
                else if (scaleType == ScaleType.Top)
                {
                    x = (width - lnNewWidth) / 2;
                    y = 0;
                }
                else if (scaleType == ScaleType.TopRight)
                {
                    x = width - lnNewWidth;
                    y = 0;
                }
                else if (scaleType == ScaleType.BottomLeft)
                {
                    x = 0;
                    y = height - lnNewHeight;
                }
                else if (scaleType == ScaleType.Bottom)
                {
                    x = (width - lnNewWidth) / 2;
                    y = height - lnNewHeight;
                }
                else if (scaleType == ScaleType.BottomRight)
                {
                    x = width - lnNewWidth;
                    y = height - lnNewHeight;
                }
                else if (scaleType == ScaleType.CenterLeft)
                {
                    x = 0;
                    y = (height - lnNewHeight) / 2;
                }
                else if (scaleType == ScaleType.Center)
                {
                    x = (width - lnNewWidth) / 2;
                    y = (height - lnNewHeight) / 2;
                }
                else if (scaleType == ScaleType.CenterRight)
                {
                    x = width - lnNewWidth;
                    y = (height - lnNewHeight) / 2;
                }
            }
            else
            {
                width = lnNewWidth;
                height = lnNewHeight;
            }

            Bitmap bmpOut;
            if (reverseSize == ReverseSize.Fixed)
                bmpOut = new Bitmap(width, height);
            else
            {
                bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
                x = 0;
                y = 0;
                width = lnNewWidth;
                height = lnNewHeight;
            }
            Graphics g = Graphics.FromImage(bmpOut);

            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.FillRectangle(brush, 0, 0, width, height);
            g.DrawImage(image, x, y, lnNewWidth, lnNewHeight);
            return bmpOut;
        }

        public static Bitmap CreateThumbnail(string filename, int width, int height)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(filename);

                bmpOut = CreateThumbnail(width, height, loBmp, ScaleType.Center, ReverseSize.Fixed, Brushes.Transparent);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(string filename, Size size)
        {
            return CreateThumbnail(filename, size.Width, size.Height);
        }

        public static Bitmap CreateThumbnail(FileUpload fileUpload, int width, int height)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(fileUpload.PostedFile.InputStream);

                bmpOut = CreateThumbnail(width, height, loBmp, ScaleType.Center, ReverseSize.Fixed, Brushes.Transparent);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(FileUpload fileUpload, Size size)
        {
            return CreateThumbnail(fileUpload, size.Width, size.Height);
        }

        public static Bitmap CreateThumbnail(Stream stream, int width, int height)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(stream);

                bmpOut = CreateThumbnail(width, height, loBmp, ScaleType.Center, ReverseSize.Fixed, Brushes.Transparent);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(Stream stream, Size size)
        {
            return CreateThumbnail(stream, size.Width, size.Height);
        }

        public static Bitmap CreateThumbnail(System.Drawing.Image image, int width, int height)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(image);

                bmpOut = CreateThumbnail(width, height, loBmp, ScaleType.Center, ReverseSize.Fixed, Brushes.Transparent);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(System.Drawing.Image image, Size size)
        {
            return CreateThumbnail(image, size.Width, size.Height);
        }

        public static Bitmap CreateThumbnail(string filename, int width, int height, Brush brush)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(filename);

                bmpOut = CreateThumbnail(width, height, loBmp, ScaleType.Center, ReverseSize.Fixed, brush);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(string filename, Size size, Brush brush)
        {
            return CreateThumbnail(filename, size.Width, size.Height, brush);
        }

        public static Bitmap CreateThumbnail(FileUpload fileUpload, int width, int height, Brush brush)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(fileUpload.PostedFile.InputStream);

                bmpOut = CreateThumbnail(width, height, loBmp, ScaleType.Center, ReverseSize.Fixed, brush);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(FileUpload fileUpload, Size size, Brush brush)
        {
            return CreateThumbnail(fileUpload, size.Width, size.Height, brush);
        }

        public static Bitmap CreateThumbnail(Stream stream, int width, int height, Brush brush)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(stream);

                bmpOut = CreateThumbnail(width, height, loBmp, ScaleType.Center, ReverseSize.Fixed, brush);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(Stream stream, Size size, Brush brush)
        {
            return CreateThumbnail(stream, size.Width, size.Height, brush);
        }

        public static Bitmap CreateThumbnail(System.Drawing.Image image, int width, int height, Brush brush)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(image);

                bmpOut = CreateThumbnail(width, height, loBmp, ScaleType.Center, ReverseSize.Fixed, brush);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(System.Drawing.Image image, Size size, Brush brush)
        {
            return CreateThumbnail(image, size.Width, size.Height, brush);
        }

        public static Bitmap CreateThumbnail(string filename, int width, int height, ReverseSize reverseSize)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(filename);

                bmpOut = CreateThumbnail(width, height, loBmp, ScaleType.Center, reverseSize, Brushes.Transparent);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(string filename, Size size, ReverseSize reverseSize)
        {
            return CreateThumbnail(filename, size.Width, size.Height, reverseSize);
        }

        public static Bitmap CreateThumbnail(FileUpload fileUpload, int width, int height, ReverseSize reverseSize)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(fileUpload.PostedFile.InputStream);

                bmpOut = CreateThumbnail(width, height, loBmp, ScaleType.Center, reverseSize, Brushes.Transparent);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(FileUpload fileUpload, Size size, ReverseSize reverseSize)
        {
            return CreateThumbnail(fileUpload, size.Width, size.Height, reverseSize);
        }

        public static Bitmap CreateThumbnail(Stream stream, int width, int height, ReverseSize reverseSize)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(stream);

                bmpOut = CreateThumbnail(width, height, loBmp, ScaleType.Center, reverseSize, Brushes.Transparent);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(Stream stream, Size size, ReverseSize reverseSize)
        {
            return CreateThumbnail(stream, size.Width, size.Height, reverseSize);
        }

        public static Bitmap CreateThumbnail(System.Drawing.Image image, int width, int height, ReverseSize reverseSize)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(image);

                bmpOut = CreateThumbnail(width, height, loBmp, ScaleType.Center, reverseSize, Brushes.Transparent);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(System.Drawing.Image image, Size size, ReverseSize reverseSize)
        {
            return CreateThumbnail(image, size.Width, size.Height, reverseSize);
        }

        public static Bitmap CreateThumbnail(string filename, int width, int height, ReverseSize reverseSize, Brush brush)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(filename);

                bmpOut = CreateThumbnail(width, height, loBmp, ScaleType.Center, reverseSize, brush);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(string filename, Size size, ReverseSize reverseSize, Brush brush)
        {
            return CreateThumbnail(filename, size.Width, size.Height, reverseSize, brush);
        }

        public static Bitmap CreateThumbnail(FileUpload fileUpload, int width, int height, ReverseSize reverseSize, Brush brush)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(fileUpload.PostedFile.InputStream);

                bmpOut = CreateThumbnail(width, height, loBmp, ScaleType.Center, reverseSize, brush);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(FileUpload fileUpload, Size size, ReverseSize reverseSize, Brush brush)
        {
            return CreateThumbnail(fileUpload, size.Width, size.Height, reverseSize, brush);
        }

        public static Bitmap CreateThumbnail(Stream stream, int width, int height, ReverseSize reverseSize, Brush brush)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(stream);

                bmpOut = CreateThumbnail(width, height, loBmp, ScaleType.Center, reverseSize, brush);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(Stream stream, Size size, ReverseSize reverseSize, Brush brush)
        {
            return CreateThumbnail(stream, size.Width, size.Height, reverseSize, brush);
        }

        public static Bitmap CreateThumbnail(System.Drawing.Image image, int width, int height, ReverseSize reverseSize, Brush brush)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(image);

                bmpOut = CreateThumbnail(width, height, loBmp, ScaleType.Center, reverseSize, brush);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(System.Drawing.Image image, Size size, ReverseSize reverseSize, Brush brush)
        {
            return CreateThumbnail(image, size.Width, size.Height, reverseSize, brush);
        }

        public static Bitmap CreateThumbnail(string filename, int width, int height, ScaleType scaleType)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(filename);

                bmpOut = CreateThumbnail(width, height, loBmp, scaleType, ReverseSize.Fixed, Brushes.Transparent);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(string filename, Size size, ScaleType scaleType)
        {
            return CreateThumbnail(filename, size.Width, size.Height, scaleType);
        }

        public static Bitmap CreateThumbnail(FileUpload fileUpload, int width, int height, ScaleType scaleType)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(fileUpload.PostedFile.InputStream);

                bmpOut = CreateThumbnail(width, height, loBmp, scaleType, ReverseSize.Fixed, Brushes.Transparent);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(FileUpload fileUpload, Size size, ScaleType scaleType)
        {
            return CreateThumbnail(fileUpload, size.Width, size.Height, scaleType);
        }

        public static Bitmap CreateThumbnail(Stream stream, int width, int height, ScaleType scaleType)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(stream);

                bmpOut = CreateThumbnail(width, height, loBmp, scaleType, ReverseSize.Fixed, Brushes.Transparent);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(Stream stream, Size size, ScaleType scaleType)
        {
            return CreateThumbnail(stream, size.Width, size.Height, scaleType);
        }

        public static Bitmap CreateThumbnail(System.Drawing.Image image, int width, int height, ScaleType scaleType)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(image);

                bmpOut = CreateThumbnail(width, height, loBmp, scaleType, ReverseSize.Fixed, Brushes.Transparent);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(System.Drawing.Image image, Size size, ScaleType scaleType)
        {
            return CreateThumbnail(image, size.Width, size.Height, scaleType);
        }

        public static Bitmap CreateThumbnail(string filename, int width, int height, ScaleType scaleType, Brush brush)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(filename);

                bmpOut = CreateThumbnail(width, height, loBmp, scaleType, ReverseSize.Fixed, brush);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(string filename, Size size, ScaleType scaleType, Brush brush)
        {
            return CreateThumbnail(filename, size.Width, size.Height, scaleType, brush);
        }

        public static Bitmap CreateThumbnail(FileUpload fileUpload, int width, int height, ScaleType scaleType, Brush brush)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(fileUpload.PostedFile.InputStream);

                bmpOut = CreateThumbnail(width, height, loBmp, scaleType, ReverseSize.Fixed, brush);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(FileUpload fileUpload, Size size, ScaleType scaleType, Brush brush)
        {
            return CreateThumbnail(fileUpload, size.Width, size.Height, scaleType, brush);
        }

        public static Bitmap CreateThumbnail(Stream stream, int width, int height, ScaleType scaleType, Brush brush)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(stream);

                bmpOut = CreateThumbnail(width, height, loBmp, scaleType, ReverseSize.Fixed, brush);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(Stream stream, Size size, ScaleType scaleType, Brush brush)
        {
            return CreateThumbnail(stream, size.Width, size.Height, scaleType, brush);
        }

        public static Bitmap CreateThumbnail(System.Drawing.Image image, int width, int height, ScaleType scaleType, Brush brush)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(image);

                bmpOut = CreateThumbnail(width, height, loBmp, scaleType, ReverseSize.Fixed, brush);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(System.Drawing.Image image, Size size, ScaleType scaleType, Brush brush)
        {
            return CreateThumbnail(image, size.Width, size.Height, scaleType, brush);
        }

        public static Bitmap CreateThumbnail(string filename, int width, int height, ScaleType scaleType, ReverseSize reverseSize)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(filename);

                bmpOut = CreateThumbnail(width, height, loBmp, scaleType, reverseSize, Brushes.Transparent);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(string filename, Size size, ScaleType scaleType, ReverseSize reverseSize)
        {
            return CreateThumbnail(filename, size.Width, size.Height, scaleType, reverseSize);
        }

        public static Bitmap CreateThumbnail(FileUpload fileUpload, int width, int height, ScaleType scaleType, ReverseSize reverseSize)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(fileUpload.PostedFile.InputStream);

                bmpOut = CreateThumbnail(width, height, loBmp, scaleType, reverseSize, Brushes.Transparent);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(FileUpload fileUpload, Size size, ScaleType scaleType, ReverseSize reverseSize)
        {
            return CreateThumbnail(fileUpload, size.Width, size.Height, scaleType, reverseSize);
        }

        public static Bitmap CreateThumbnail(Stream stream, int width, int height, ScaleType scaleType, ReverseSize reverseSize)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(stream);

                bmpOut = CreateThumbnail(width, height, loBmp, scaleType, reverseSize, Brushes.Transparent);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(Stream stream, Size size, ScaleType scaleType, ReverseSize reverseSize)
        {
            return CreateThumbnail(stream, size.Width, size.Height, scaleType, reverseSize);
        }

        public static Bitmap CreateThumbnail(System.Drawing.Image image, int width, int height, ScaleType scaleType, ReverseSize reverseSize)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(image);

                bmpOut = CreateThumbnail(width, height, loBmp, scaleType, reverseSize, Brushes.Transparent);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(System.Drawing.Image image, Size size, ScaleType scaleType, ReverseSize reverseSize)
        {
            return CreateThumbnail(image, size.Width, size.Height, scaleType, reverseSize);
        }

        public static Bitmap CreateThumbnail(string filename, int width, int height, ScaleType scaleType, ReverseSize reverseSize, Brush brush)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(filename);

                bmpOut = CreateThumbnail(width, height, loBmp, scaleType, reverseSize, brush);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(string filename, Size size, ScaleType scaleType, ReverseSize reverseSize, Brush brush)
        {
            return CreateThumbnail(filename, size.Width, size.Height, scaleType, reverseSize, brush);
        }

        public static Bitmap CreateThumbnail(FileUpload fileUpload, int width, int height, ScaleType scaleType, ReverseSize reverseSize, Brush brush)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(fileUpload.PostedFile.InputStream);

                bmpOut = CreateThumbnail(width, height, loBmp, scaleType, reverseSize, brush);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(FileUpload fileUpload, Size size, ScaleType scaleType, ReverseSize reverseSize, Brush brush)
        {
            return CreateThumbnail(fileUpload, size.Width, size.Height, scaleType, reverseSize, brush);
        }

        public static Bitmap CreateThumbnail(Stream stream, int width, int height, ScaleType scaleType, ReverseSize reverseSize, Brush brush)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(stream);

                bmpOut = CreateThumbnail(width, height, loBmp, scaleType, reverseSize, brush);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(Stream stream, Size size, ScaleType scaleType, ReverseSize reverseSize, Brush brush)
        {
            return CreateThumbnail(stream, size.Width, size.Height, scaleType, reverseSize, brush);
        }

        public static Bitmap CreateThumbnail(System.Drawing.Image image, int width, int height, ScaleType scaleType, ReverseSize reverseSize, Brush brush)
        {
            Bitmap bmpOut;
            try
            {
                var loBmp = new Bitmap(image);

                bmpOut = CreateThumbnail(width, height, loBmp, scaleType, reverseSize, brush);

                loBmp.Dispose();
            }
            catch
            {
                return null;
            }

            return bmpOut;
        }

        public static Bitmap CreateThumbnail(System.Drawing.Image image, Size size, ScaleType scaleType, ReverseSize reverseSize, Brush brush)
        {
            return CreateThumbnail(image, size.Width, size.Height, scaleType, reverseSize, brush);
        }

    }
}